﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.ApplicationFramework;
using MyPublisher.Administration.Commands;
using System.Collections.ObjectModel;
using MyPublisher.Administration.Views;
using MyPublisher.Administration.Model;

namespace MyPublisher.Administration.ViewModels
{
    public class SplashItem : System.Windows.ApplicationFramework.Model
    {
        public string Form { get; set; }
        public string Action { get; set; }
        public string Name { get; set; }
        public SplashItem() { }

        private bool _isVisible;

        public bool IsVisible
        {
            get { return _isVisible; }
            set
            {
                _isVisible = value;
                RaisePropertyChanged("IsVisible");
            }
        }
    }

    public class MainViewModel : ViewModel<IView>
    {
        public ObservableCollection<object> DocumentViews { get; private set; }
        private DelegateCommand exitCommand;
        private DelegateCommand closeCommand;
        private DelegateCommand openCommand;
        public event EventHandler Closed;
        public event EventHandler Opend;
        #region Constructor

        public MainViewModel(IView view)
            :base(view)
        {
            this.DocumentViews = new ObservableCollection<object>();
        }

        #endregion
        private object activeDocumentView;
        public object ActiveDocumentView
        {
            get { return activeDocumentView; }
            set
            {
                if (activeDocumentView != value)
                {
                    activeDocumentView = value;
                    RaisePropertyChanged("ActiveDocumentView");
                }
            }
        }

        public ICommand ExitCommand
        {
            get
            {
                if (exitCommand == null)
                {
                    exitCommand = new DelegateCommand(Exit);
                }
                return exitCommand;
            }
        }
        public ICommand CloseCommand
        {
            get
            {
                if (closeCommand == null)
                {
                    closeCommand = new DelegateCommand(Close);
                }
                return closeCommand;
            }
        }
        public ICommand OpenCommand
        {
            get
            {
                if (openCommand == null)
                {
                    openCommand = new DelegateCommand(Open);
                }
                return openCommand;
            }
        }

        public void Show()
        {
            (View as MainView).Show();
        }

        private void Exit()
        {
            Application.Current.Shutdown();
        }

        private void Close()
        {
            if (DocumentViews.IndexOf(ActiveDocumentView) != -1)
            {
                DocumentViews.Remove(ActiveDocumentView);
                if (Closed != null)
                {
                    Closed(this, new EventArgs());
                }
            }
        }

        public void Open(object parameter)
        {
            ViewModel model = null;
            string name = parameter as string;

            IView view = this.DocumentViews.SingleOrDefault((v) => 
                {
                    IDocumentViewModel viewModel = (v as IView).DataContext as IDocumentViewModel;
                    return viewModel.Document.Name == name;
                }) as IView;

            if (view != null)
            {
                IDocumentViewModel documentViewModel = view.DataContext as IDocumentViewModel;
                if (documentViewModel != null && !documentViewModel.Document.Multiple)
                {
                    int index = DocumentViews.IndexOf(view);
                    this.ActiveDocumentView = DocumentViews[index];
                    return;
                }
            }

            switch (name)
            {
                case "Administration":
                    model = new AdminViewModel(new AdminView(), name);
                    break;
                case "Publishing":
                    model = new PublishingViewModel(new PublishingView(), name);
                    break;
                case "Package":
                    model = new PackageViewModel(new PackageView(), name);
                    break;
                case "File":
                    model = new FileViewModel(new FileView(), name);
                    break;
                case "Document":
                    model = new ContentViewModel(new ContentView(), name);
                    break;
                default:
                    break;
            }

            if (model != null)
            {
                this.DocumentViews.Add(model.View);
                this.ActiveDocumentView = DocumentViews[DocumentViews.Count - 1];
                if (Opend != null)
                {
                    Opend(this, new EventArgs());
                }
            }
        }
    }
}
